package com.gongpingjialoan.activity.loans;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.activity.main.BaseActivity;
import com.gongpingjialoan.api.API;
import com.gongpingjialoan.bean.LoanStatusData;
import com.gongpingjialoan.network.NetDataJson;
import com.gongpingjialoan.utility.JSONUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 放款凭证
 */
public class LoanDocumentsActivity extends BaseActivity {
    //贷款选项
    private LinearLayout loanOtionsV;

    private TextView credit_limitT,nameT,identity_card_noT,credit_dateT;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loan_documents);
        initView();
    }

    private void initView() {
        setTitle("放款凭证");
        credit_limitT = (TextView) findViewById(R.id.credit_limit);
        nameT = (TextView) findViewById(R.id.name);
        identity_card_noT = (TextView) findViewById(R.id.identity_card_no);
        loanOtionsV = (LinearLayout) findViewById(R.id.loan_opotions);
        credit_dateT = (TextView) findViewById(R.id.credit_date);

        getloanoptions(LoanStatusData.getInstance().getCredit_limit());
        java.text.DecimalFormat df = new java.text.DecimalFormat("##,###,###");
        credit_limitT.setText(df.format(Float.valueOf(LoanStatusData.getInstance().getCredit_limit())));
        nameT.setText(LoanStatusData.getInstance().getName());
        identity_card_noT.setText(LoanStatusData.getInstance().getIdentity_card_no());
        String credit_date = LoanStatusData.getInstance().getCredit_date();
        if(!TextUtils.isEmpty(credit_date)){
            credit_dateT.setText("本凭证有效期至：" + credit_date);
        }
    }

    //获取贷款选项
    private void getloanoptions(String money) {
        showProgressDialog();
        NetDataJson netDataJson = new NetDataJson(new NetDataJson.OnStringListener() {
            @Override
            public void onDataJsonError(String errorMessage) {
                hidenProgressDialog();
            }

            @Override
            public void onDataJsonUpdate(String json) {
                hidenProgressDialog();
                try {
                    JSONObject jo = new JSONObject(json);
                    JSONArray jsa = jo.getJSONArray("options");
                    bindLoadOptions(jsa);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, 1);
        netDataJson.setUrl(NetDataJson.NEWCAR_SERVER_DOMAIN + API.new_car_detail_options);
        netDataJson.addParam("money", money);
        netDataJson.setUseOtherDomain(true);
        netDataJson.request("get");
    }

    private void bindLoadOptions(JSONArray jsonArray) {
        loanOtionsV.removeAllViews();
        LayoutInflater mLayoutInflater = LayoutInflater.from(mySelf);

        for (int i = 0; i < jsonArray.length(); i++) {
            try {
                JSONObject jo = jsonArray.getJSONObject(i);
                View childV = mLayoutInflater.inflate(R.layout.item_loan_documents, null);
                loanOtionsV.addView(childV);
                TextView proportionT = (TextView) childV.findViewById(R.id.proportion);
                TextView month_payT = (TextView) childV.findViewById(R.id.month_pay);
                TextView monthsT = (TextView) childV.findViewById(R.id.months);
                TextView nameT = (TextView) childV.findViewById(R.id.name);
                TextView rateT = (TextView) childV.findViewById(R.id.rate);

                rateT.setText(jo.getString("rate"));
                int advance = (int)Math.rint(JSONUtil.getFloat(jo,"advance"));
                java.text.DecimalFormat df = new java.text.DecimalFormat("##,###,###");
                proportionT.setText(df.format(advance) + "元");
                month_payT.setText(jo.getString("month_pay") + "元");
                monthsT.setText(jo.getString("months") + "个月");
                nameT.setText(jo.getString("name"));

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

    }
}
